// pages/member/address.js
var util = require("../../../utils/util.js");

//获取应用实例
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      coupon: [],
      showQrcode: false,
      qrCodeUrl: '',
      page: 1,
      total_page: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.log(options);
      this.getCouponList();
    },


    showQrcode(e) {
      let that = this;
      let index = e.currentTarget.dataset.index;
      console.log(index);
      app.httpPost('member/qcode', {
          'width': 200,
          'page': 'pages/member/form/form',
          'params': {
              coupon_id: that.data.coupon[index].id,
          },
      }, json => {
          if(json.data.error==0){
              that.setData({
                  showQrcode: true,
                  qrCodeUrl: app.globalData.imgDir+json.data.url
              })
          }
      })

    },

    hideModal(e) {
      this.setData({
        showQrcode: false
      })
    },

      getCouponList(e) {
          let that = this;
          app.httpPost('member/coupon', {
            'page': that.data.page
          }, json => {
              if(json.code==0){
                  that.setData({
                    coupon: that.data.coupon.concat(json.data),
                    total_page: json.total_page,
                  });
              }
          })
      },

      onReachBottom: function () {
        let that = this;
        if(that.data.page == that.data.total_page) {
          return ;
        }
        that.setData({
          page: that.data.page + 1,
        })
        that.getCouponList();
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

})